﻿using DebugAnalyzer.ObjectModel;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;

namespace Analysis.DumpSummary
{
    public class DumpSummary : AnalysisPlug
    {
        public override void RunAnalysis()
        {
            BeginReport("Dump Summary");

            WriteHeadLine("Dump Information");
            StringBuilder str = new StringBuilder();
            str.Append("<TABLE border='0' cellspacing='0' cellpadding='1' class='datatable'>");
            str.AppendFormat("<TR><TD>OS Version</TD><TD>{0}</TD></TR>", Analyzer.DBG.SystemVersion);
            str.AppendFormat("<TR><TD>Dump Type</TD><TD>{0}</TD></TR>", Analyzer.DBG.DumpType);
            str.AppendFormat("<TR><TD>Debugee Type</TD><TD>{0}</TD></TR>", Analyzer.DBG.DebugeeType);
            str.AppendFormat("<TR><TD>Processor</TD><TD>{0}</TD></TR>", Analyzer.DBG.ProcessType);
            str.AppendFormat("<TR><TD>System Uptime</TD><TD>{0}</TD></TR>", Analyzer.DBG.SystemUptime);
            str.AppendFormat("<TR><TD>Process Uptime</TD><TD>{0}</TD></TR>", Analyzer.DBG.ProcessUptime);
            str.AppendFormat("<TR><TD>Dump Filesize</TD><TD>{0}</TD></TR>", Analyzer.DBG.DataFileSize.ToByteString());
            str.AppendFormat("<TR><TD>Comment</TD><TD>{0}</TD></TR>", Analyzer.DBG.Comment);
            str.Append("</TABLE>");
            WriteHtmlLine(str.ToString());
            str.Clear();

            WriteBlankLine();
            WriteHeadLine(".NET Runtime");

            str.Append("<TABLE border='0' cellspacing='0' cellpadding='1' class='datatable'>");
            str.AppendFormat("<TR><TD>CLR Version</TD><TD>{0}</TD></TR>", Analyzer.CLR.CLRVersion.ToString());
            DbgModule nm = Analyzer.DBG.DbgModuleCollection["mscorwks"];
            str.AppendFormat("<TR><TD>EE Version</TD><TD>{0} ({1})</TD></TR>", nm.FileVersion.ToString(), nm.Comments);
            str.Append("</TABLE>");
            WriteHtmlLine(str.ToString());

            // *** Managed or Native memory issue info ***
            // double totalHeapSize = Analyzer.CLR.ClrHeapCollection.Sum(s => s.TotalSize);
            // WriteRecommendation(ResultSeverity.Information,
            //    ".NET Heap size is " + totalHeapSize.ToByteString() + " and Total dump size is " + Analyzer.DBG.DataFileSize.ToByteString(),
            //    "If you are troubleshooting memory issue and there is larger difference between these two sizes, the issue could be related to Native memory.<br/>" +
            //    "(Tip: Applicable only for Full Memory Dump files)"
            //    );

            EndReport();
        }
    }
}
